package com.lzx.dormsys.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.lzx.dormsys.bean.SysDeptBean;
import com.lzx.dormsys.core.TreeSelect;
import com.lzx.dormsys.util.DateUtils;
import com.lzx.dormsys.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lzx.dormsys.mapper.SysDeptBeanMapper;
import com.lzx.dormsys.service.ISysDeptBeanService;

/**
 * 部门Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class SysDeptBeanServiceImpl implements ISysDeptBeanService {
    @Autowired
    private SysDeptBeanMapper sysDeptBeanMapper;

    /**
     * 查询部门
     *
     * @param deptId 部门ID
     * @return 部门
     */
    @Override
    public SysDeptBean selectSysDeptBeanById(Long deptId) {
        return sysDeptBeanMapper.selectSysDeptBeanById(deptId);
    }

    /**
     * 查询部门列表
     *
     * @param sysDeptBean 部门
     * @return 部门
     */
    @Override
    public List<SysDeptBean> selectSysDeptBeanList(SysDeptBean sysDeptBean) {
        return sysDeptBeanMapper.selectSysDeptBeanList(sysDeptBean);
    }

    /**
     * 新增部门
     *
     * @param sysDeptBean 部门
     * @return 结果
     */
    @Override
    public int insertSysDeptBean(SysDeptBean sysDeptBean) {
        sysDeptBean.setCreateTime(DateUtils.getNowDate());
        return sysDeptBeanMapper.insertSysDeptBean(sysDeptBean);
    }

    /**
     * 修改部门
     *
     * @param sysDeptBean 部门
     * @return 结果
     */
    @Override
    public int updateSysDeptBean(SysDeptBean sysDeptBean) {
        sysDeptBean.setUpdateTime(DateUtils.getNowDate());
        return sysDeptBeanMapper.updateSysDeptBean(sysDeptBean);
    }

    /**
     * 批量删除部门
     *
     * @param deptIds 需要删除的部门ID
     * @return 结果
     */
    @Override
    public int deleteSysDeptBeanByIds(Long[] deptIds) {
        return sysDeptBeanMapper.deleteSysDeptBeanByIds(deptIds);
    }

    /**
     * 删除部门信息
     *
     * @param deptId 部门ID
     * @return 结果
     */
    @Override
    public int deleteSysDeptBeanById(Long deptId) {
        return sysDeptBeanMapper.deleteSysDeptBeanById(deptId);
    }

    @Override
    public List<SysDeptBean> buildDeptTree(List<SysDeptBean> depts) {
        List<SysDeptBean> returnList = new ArrayList<>();
        List<Long> tempList = new ArrayList<>();
        for (SysDeptBean dept : depts) {
            tempList.add(dept.getDeptId());
        }
        for (Iterator<SysDeptBean> iterator = depts.iterator(); iterator.hasNext(); ) {
            SysDeptBean deptBean = iterator.next();
            if (!tempList.contains(deptBean.getParentId())) {
                recursionFn(depts, deptBean);
                returnList.add(deptBean);
            }
        }
        if (returnList.isEmpty()) {
            returnList = depts;
        }

        return returnList;
    }

    @Override
    public List<TreeSelect> buildDeptTreeSelect(List<SysDeptBean> depts) {
        List<SysDeptBean> sysDeptBeans = buildDeptTree(depts);
        return sysDeptBeans.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<SysDeptBean> list, SysDeptBean t) {
        List<SysDeptBean> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysDeptBean tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 得到子节点列表
     *
     * @param list
     * @param parentDept
     * @return
     */
    private List<SysDeptBean> getChildList(List<SysDeptBean> list, SysDeptBean parentDept) {
        List<SysDeptBean> tlist = new ArrayList<>();
        Iterator<SysDeptBean> it = list.iterator();
        while (it.hasNext()) {
            SysDeptBean deptBean = it.next();
            if (StringUtils.isNotNull(deptBean.getParentId()) && deptBean.getParentId().longValue() == parentDept.getDeptId().longValue()) {
                tlist.add(deptBean);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysDeptBean> list, SysDeptBean t) {
        return getChildList(list, t).size() > 0 ? true : false;
    }
}
